package com.ardan1.client.core;

import java.util.ArrayList;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;

import com.ardan1.client.core.TestEditorView.Driver;
import com.ardan1.client.editor.PersonEditor;
import com.ardan1.client.place.NameTokens;
import com.ardan1.client.validation.ClientGroup;
import com.ardan1.shared.Address;
import com.ardan1.shared.Person;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent;

public class TestEditorPresenter extends Presenter<TestEditorPresenter.MyView, TestEditorPresenter.MyProxy> {

	public interface MyView extends View {
		Driver getDriver();
		PersonEditor getPersonEditor();
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.editor)
	public interface MyProxy extends ProxyPlace<TestEditorPresenter> {
	}

	final Validator validator;
	
	@Inject
	public TestEditorPresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
		super(eventBus, view, proxy);
		 validator = Validation.buildDefaultValidatorFactory().getValidator();
	}

	@Override
	protected void revealInParent() {
		RevealRootLayoutContentEvent.fire(this, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		
		Person p = new Person();
		p.setName("Seamus");
		
		p.setAddress(new Address());
		
		getView().getPersonEditor().addSaveClickHandler(new ClickHandler() {
	        @Override
	        public void onClick(ClickEvent event) {
	            Person edited = getView().getDriver().flush();
	            
	            Set<ConstraintViolation<Person>> violations = validator.validate(edited, 
	                    Default.class, ClientGroup.class);

	            if (!violations.isEmpty()) {
	            	getView().getDriver().setConstraintViolations(new ArrayList<ConstraintViolation<?>>(violations));
	                StringBuffer errorMessage = new StringBuffer();
	                for (ConstraintViolation constraintViolation : violations) {
	                    errorMessage.append(constraintViolation.getMessage());
	                    errorMessage.append('\n');
	                }
	                GWT.log(errorMessage.toString());
	            }
	            
	            if (getView().getDriver().hasErrors()) {
	                GWT.log("Has errors");
	                return;
	            }
	            GWT.log("Edited Name: " + edited.getName());
	            GWT.log("Edited City: " + edited.getAddress().getCity());
	        }
	    });
		getView().getDriver().initialize(getView().getPersonEditor());
		getView().getDriver().edit(p);
		
		
	}
}
